<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/1
 * Time: 17:06
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\common\utils\Constant;
use app\index\logic\AnswerLogic;
use app\index\logic\AnswerLogLogic;
use app\index\logic\MessageLogic;
use app\index\logic\MoneyLogLogic;
use app\index\logic\RedEnvelopeLogic;
use think\cache\driver\Redis;
use think\Log;

class Challenge extends Frontend
{
    /**
     * ajax获取挑战规则列表
     */
    public function ajax_challenge_rules(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $list = model('Standard')->order('money','asc')->select();
        return $this->returnJson('',Constant::CODE_SUCCESS,$list);
    }

    /**
     * ajax获取挑战列表
     */
    public function ajax_challenge_list(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $data['page_size'] = $this->request->post('page_size');
        $data['page_num'] = $this->request->post('page_num');
        $data['status'] = $this->request->post('status');
        $data['is_visible'] = 1;
        $logic = new RedEnvelopeLogic();
        $result = $logic->getRedEnvelopeList($data,true);
        return $this->returnJson('',Constant::CODE_SUCCESS,$result);
    }

    /**
     * ajax获取我的挑战列表
     */
    public function ajax_my_challenge_list(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $data['page_size'] = $this->request->post('page_size');
        $data['page_num'] = $this->request->post('page_num');
        $user_id = $result['user_id'];
        $data['user_id'] = $user_id;
        $logic = new RedEnvelopeLogic();
        $result = $logic->getRedEnvelopeList($data);
        $withdraw_time = model('Config')->where(array('name'=>Constant::CONFIG_NAME_WITHDRAW_TIME))->value('value');
        $now = time();
        foreach ($result['list'] as $key=>&$value){
            if($value['status'] != Constant::RED_ENVELOPE_STATUS_NO_CHALLENGE){
                $value['can_be_recall'] = 0;
                continue;
            }
            $timediff = $now - $value['create_time'];
            $days = intval($timediff/86400);
            $remain = $timediff % 86400;
            $hours = intval($remain / 3600);
            if($days>0) {
                $hours += $days * 24;
            }
            if($hours >= $withdraw_time){
                $value['can_be_recall'] = 1;
            }else{
                $value['can_be_recall'] = 0;
            }
        }
        unset($value);
        return $this->returnJson('',Constant::CODE_SUCCESS,$result);
    }


    /**
     * ajax撤回红包
     */
    public function ajax_withdraw(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $user_id = $result['user_id'];
        $red_envelope_id = $this->request->post('red_envelope_id');
        $logic = new RedEnvelopeLogic();
        $result = $logic->withdraw($red_envelope_id,$user_id);
        $code = 0;
        $msg = '';
        $data = [];
        switch ($result['status']){
            case 0:
                $code = Constant::CODE_ERROR;
                $msg = '撤回失败！';
                $data = $result['error'];
                break;
            case 1:
                $code = Constant::CODE_SUCCESS;
                $msg = '撤回成功！';
                break;
            case 2:
                $code = Constant::CODE_ERROR;
                $msg = $result['msg'];
                break;
        }
        return $this->returnJson($msg,$code,$data);
    }

    /**
     * ajax获取挑战卡记录列表
     */
    public function ajax_card_log_list(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $data['page_size'] = $this->request->post('page_size');
        $data['page_num'] = $this->request->post('page_num');
        $user_id = $result['user_id'];
        $data['user_id'] = $user_id;
        $data['money_type'] = Constant::MONEY_TYPE_CHALLENGE_CARD;//答题金
        $logic = new MoneyLogLogic();
        $result = $logic->getMoneyLogList($data);
        return $this->returnJson('',Constant::CODE_SUCCESS,$result);
    }

    /**
     * ajax获取附近红包列表
     * @return \think\response\Json
     */
    public function ajax_near_challenge_list(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
//        $number = $this->request->post('number');
//        if(empty($number) || !is_numeric($number)){
//            $number = 8;
//        }
        $logic = new RedEnvelopeLogic();
        $result = $logic->getNearReadEnvelopeList();
        return $this->returnJson(count($result),Constant::CODE_SUCCESS,$result);
    }

    /**
     * ajax根据红包id获取答题记录
     * @return \think\response\Json
     */
    public function ajax_answer_log(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $data['page_size'] = $this->request->post('page_size');
        $data['page_num'] = $this->request->post('page_num');
        $data['red_envelope_id'] = $this->request->post('red_envelope_id');
        $data['status'] = Constant::ANSWER_STATUS_END;
        $logic = new AnswerLogLogic();
        $result = $logic->getAnswerLogList($data);
        return $this->returnJson('',Constant::CODE_SUCCESS,$result);
    }

    /**
     * ajax获取红包详情
     * @return \think\response\Json
     */
    public function ajax_challenge_detail(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $red_envelope_id = $this->request->post('red_envelope_id');
        $logic = new RedEnvelopeLogic();
        $result = $logic->getRedEnvelopeById($red_envelope_id);
        if($result['status'] == 1){
            return $this->returnJson('',Constant::CODE_SUCCESS,$result['data']);
        }else{
            return $this->returnJson($result['msg'],Constant::CODE_ERROR);
        }
    }

    /**
     * ajax获取用户历史挑战卡数目
     */
    public function ajax_history_card(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $user_id = $result['user_id'];
        $history_answer_money = model('User')->where(array('id'=>$user_id))->value('history_answer_money');
        $data['history_answer_money'] = $history_answer_money;
        return $this->returnJson('',Constant::CODE_SUCCESS,$data);
    }

    /**
     * ajax开始答题
     */
    public function ajax_start_answer(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $user_id = $result['user_id'];
        $answer_log_id = $this->request->post('answer_log_id');
        $logic = new AnswerLogic();
        $result = $logic->doStartAnswer($answer_log_id,$user_id);
        $code = 0;
        $msg = '';
        $data = [];
        switch ($result['status']){
            case 0:
                $code = Constant::CODE_ERROR;
                $msg = '开始答题失败！';
                $data = $result['error'];
                break;
            case 1:
                $code = Constant::CODE_SUCCESS;
                $msg = '开始答题！';
                $data = $result['data'];
                $topic_id = $data['topic_id'];
                if(empty($topic_id)){
                    $topic_id = 0;
                }
                pclose(popen("php /www/RedEnvelope/public/index.php /index/challenge/auto_answer/user_id/{$user_id}/answer_log_id/{$answer_log_id}/topic_id/{$topic_id} &", 'r'));
//                $logic->answerTime($answer_log_id,$topic_id,$user_id);
                break;
            case 2:
                $code = Constant::CODE_ERROR;
                $msg = $result['msg'];
                break;
        }
        return $this->returnJson($msg,$code,$data);
//        model('AnswerLog')->where(array)
    }


    /**
     * ajax提交答案
     * @return \think\response\Json
     */
    public function ajax_submit_answer(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $user_id = $result['user_id'];
        $answer_log_id = $this->request->post('answer_log_id');
        $topic_id = $this->request->post('topic_id');
        $option_id = $this->request->post('option_id');
        $logic = new AnswerLogic();
        $result = $logic->checkAnswer($user_id,$answer_log_id,$topic_id,$option_id);
        $code = 0;
        $msg = '';
        $data = [];
        switch ($result['status']){
            case 0:
                $code = Constant::CODE_ERROR;
                $msg = '答题失败！';
                $data = $result['error'];
                break;
            case 1:
                $code = Constant::CODE_SUCCESS;
                $msg = '答题成功！';
                $data = $result['data'];
                break;
            case 2:
                $code = Constant::CODE_ERROR;
                $msg = $result['msg'];
                break;
        }
        return $this->returnJson($msg,$code,$data);
    }


    /**
     * 执行自动结束答题操作
     * @param $user_id
     * @param $answer_log_id
     * @param $topic_id
     */
    public function auto_answer($user_id,$answer_log_id,$topic_id){
        if(!$this->request->isCli()){
            die();
        }
        $logic = new AnswerLogic();
        $logic->answerTime($answer_log_id,$topic_id,$user_id);
    }

    /**
     * 支付后长时间未答题自动结算
     * @param $user_id
     * @param $answer_log_id
     */
    public function auto_end_answer($user_id,$answer_log_id){
        if(!$this->request->isCli()){
            die();
        }
        $logic = new AnswerLogic();
        $logic->autoEndAnswer($answer_log_id,$user_id);
    }

}